package com.wuxiaolong.wechat.service.impl;

import com.alibaba.fastjson.JSON;
import com.baidu.aip.ocr.AipOcr;
import com.wuxiaolong.wechat.model.BaseMessage;
import com.wuxiaolong.wechat.model.NewsItem;
import com.wuxiaolong.wechat.model.NewsMessage;
import com.wuxiaolong.wechat.model.TextMessage;
import com.wuxiaolong.wechat.model.baidu.CharacterRecognitionResp;
import com.wuxiaolong.wechat.service.BaiduService;
import com.wuxiaolong.wechat.service.JuheService;
import com.wuxiaolong.wechat.service.MessageService;
import com.wuxiaolong.wechat.util.BeanXmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:
 *
 * @author Leo
 * @date 2020-02-13
 */
@Service
@Slf4j
public class MessageServiceImpl implements MessageService {

    @Autowired
    private JuheService juheService;

    @Autowired
    private BaiduService baiduService;

    /**
     * 根据请求消息的内容获取响应消息的xml str
     * @param requestMap
     * @return
     */
    @Override
    public String getResponseXmlStr(Map<String, String> requestMap) {

        BaseMessage respMsg = null;

        String msgType = requestMap.get("MsgType");
        switch (msgType){
            case "text":
                respMsg = dealTextMessage(requestMap);
                break;
            case "image":
                respMsg = dealImageMessage(requestMap);
                break;
            case "voice":
                break;
            case "video":
                break;
            case "music":
                break;
            case "news":
                break;
            // 事件处理：todo

            default:
                break;
        }
        log.info("resp:"+ JSON.toJSONString(respMsg));

        if(respMsg != null){
            return BeanXmlUtil.beanToXmlStr(respMsg);
        }

        return null;
    }

    /**
     * 处理图片消息
     * @param requestMap
     * @return
     */
    private BaseMessage dealImageMessage(Map<String, String> requestMap) {

        // 百度图片文字识别
        String resp = baiduService.characterRecognition(requestMap);

        TextMessage tm = new TextMessage(requestMap,resp);

        return tm;
    }

    /**
     * 处理文字消息
     * @param requestMap
     * @return
     */
    private BaseMessage dealTextMessage(Map<String, String> requestMap) {

        //测试回复图文消息
        String content = requestMap.get("Content");
        if("图文".equals(content)){
            return dealNewsMessage(requestMap);
        }

        if("精选".equals(content)){
            return juheService.weChatSelection(requestMap);
        }

        if(content.matches("^.*油价.*$")){
            return juheService.todayOilPrice(requestMap);
        }

        if("头条".equals(content)){
            return juheService.headlineNews(requestMap);
        }

        // 聊天机器人
        String resp = juheService.chat(requestMap);

        TextMessage tm = new TextMessage(requestMap,resp);

        return tm;
    }

    private BaseMessage dealNewsMessage(Map<String, String> requestMap) {
        List<NewsItem> articles = new ArrayList<>();
        articles.add(new NewsItem("test1","desc1","https://pic1.zhimg.com/v2-9df1fb7690d373b2e41ceec409993574_400x224.jpg","https://mp.weixin.qq.com/s/vjNAzrmaxhitI9ra7FenkA"));
        articles.add(new NewsItem("test2","desc2","https://pic4.zhimg.com/v2-11166ed733e4b24898edf4da6bbeeb50_400x224.jpg","https://mp.weixin.qq.com/s/15ZwxBbYeU0bOOAWdB8U0g"));
        NewsMessage nm = new NewsMessage(requestMap,articles);
        return nm;
    }
}
